package com.heatup.apt.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.heatup.apt.api.service.TSysPresaleTeamService;
import com.heatup.apt.api.service.UserService;
import com.heatup.apt.common.dao.OrderInfoMapper;
import com.heatup.apt.common.dao.TSysPresaleTeamMapper;
import com.heatup.apt.model.PresaleTeamUserApiModel;
import com.heatup.apt.model.TSysPresaleTeam;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.order.OrderCoachPrivate;
import com.heatup.apt.model.user.UserInfo;

/**
 *
 * TSysPresaleTeam 表数据服务层接口实现类
 *
 */
@Service
public class TSysPresaleTeamServiceImpl extends SuperServiceImpl<TSysPresaleTeamMapper, TSysPresaleTeam> implements TSysPresaleTeamService {
	
	Logger logger = LoggerFactory.getLogger(TSysPresaleTeamServiceImpl.class);
	
	@Autowired
	private UserService userService;
	
	@Autowired
	TSysPresaleTeamMapper tSysPresaleTeamMapper;
	
	@Autowired
	OrderInfoMapper orderInfoMapper;
	
	/**
	 * 添加会籍人员
	 * @throws Exception 
	 */
	@Override
	public ApiResultModel<Object> addPresaleUser(String userName, String mobile, UserInfo adminUserInfo) throws Exception {
		// TODO Auto-generated method stub
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		UserInfo userInfo = userService.getUserByMobile(mobile);
		
		// 参数校验
		String msg = checkAddPresaleUserPram(userName, mobile, adminUserInfo, userInfo);
		if(!StringUtils.isEmpty(msg)) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg(msg);
			return apiResultModel;
		}
		
		// 创建预售人员
		TSysPresaleTeam userPresaleTeam = new TSysPresaleTeam();
		userPresaleTeam.setMobile(mobile);
		userPresaleTeam.setUserId(userInfo.getId());
		userPresaleTeam.setUserName(userName);
		userPresaleTeam.setParentUserId(adminUserInfo.getId());
		userPresaleTeam.setCreateBy(adminUserInfo.getId()+"");
		userPresaleTeam.setCreateTime(new Date());
		userPresaleTeam.setRole("user");
		Integer addCount = tSysPresaleTeamMapper.insertSelective(userPresaleTeam);
		
		// 插入
		if(addCount > 0) {
			apiResultModel.setRet(apiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("添加成功");
		}else {
			apiResultModel.setRet(apiResultModel.RET_ERROR);
			apiResultModel.setMsg("添加失败");
		}
		return apiResultModel;
	}
	
	/**
	 * 添加预售人员 校验参数以及权限
	 * @param userName
	 * @param mobile
	 * @param adminUserInfo
	 * @param userInfo
	 * @return
	 */
	public String checkAddPresaleUserPram(String userName, String mobile, UserInfo adminUserInfo, UserInfo userInfo) {
		
		// stpe 1 判断该手机号码是否存在用户
		if (userInfo == null) {
			return "用户未绑定手机号码,请先到微信公众账号绑定手机号码";
		}
		if (userInfo.getSubscribe() != 1) {
			return "该用户未关注微信公众账号";
		}

		// step 2 判断添加人员是否有权限
		TSysPresaleTeam presaleTeam = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(adminUserInfo.getId(),
				null);
		if (presaleTeam == null) {
			return "对不起，您没有该权限";
		}

		if (presaleTeam.getParentUserId().intValue() != adminUserInfo.getId().intValue()) {
			return "对不起，您没有该权限";
		}
		
		if (!"admin".equals(presaleTeam.getRole())) {
			return "对不起，您没有该权限";
		}

		// step 3 判断改手机号码是否已经是预售团队并且状态是正常的用户
		TSysPresaleTeam userPresaleTeamOld = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(0, mobile);

		if (userPresaleTeamOld != null) {
			return "该用户已存在";
		}
		return null;
	}
	
	/**
	 * 查询预售团队的所有人
	 */
	@Override
	public ApiResultModel<Object> queryPresaleUserList(UserInfo userInfo) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		// 只有会籍顾问经理才有权限查看全部
		// step 2 判断添加人员是否有权限
		TSysPresaleTeam presaleTeam = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(userInfo.getId(),
				null);
		
		// 不是预售人员
		if(presaleTeam == null) {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您不是预售人员！");
			return apiResultModel;
		}
		
		// 不是会籍顾问经理
		if (presaleTeam.getParentUserId().intValue() != userInfo.getId().intValue()) {
			// 
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您不是会籍经理没有该权限！");
			return apiResultModel;
		}
		
		// 不是会籍顾问经理
		if (!"admin".equals(presaleTeam.getRole())) {
			//
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您不是会籍经理没有该权限！");
			return apiResultModel;
		}
		// 可用查看全部会籍人员包括会籍经理
		Map<String,Object> resultMap = new HashMap<>();
		List<PresaleTeamUserApiModel> teamList = tSysPresaleTeamMapper.queryPresaleTeamAll(userInfo.getId());	
		resultMap.put("presaleTeamList", teamList);
		apiResultModel.setData(resultMap);
		apiResultModel.setRet(apiResultModel.RET_SUCCESS);
		apiResultModel.setMsg("查询成功！");
		return apiResultModel;
	}
	
	/**
	 * 删除预售人员
	 */
	@Override
	public ApiResultModel<Object> delPresalUser(UserInfo adminUserInfo, Integer presaleId) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		Map<String,Object> resultMap = new HashMap<>();
		// 会籍顾问经理
		TSysPresaleTeam adminPresaleTeam = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(adminUserInfo.getId(),
				null);
		
		// 删除的会籍顾问
		TSysPresaleTeam presaleTeamUserOld = tSysPresaleTeamMapper.selectById(presaleId);
		
		// 不是预售人员
		if (adminPresaleTeam == null) {
			//
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您不是会籍顾问经理没有该权限！");
			return apiResultModel;
		}

		// 不是超级管理员
		if (!"admin".equals(adminPresaleTeam.getRole())) {
			//
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您不是会籍顾问经理没有该权限！");
			return apiResultModel;
		}

		// 删除的人员不存在
		if (presaleTeamUserOld == null || (presaleTeamUserOld != null && presaleTeamUserOld.getStatus() == 0)) {
			//
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您删除的人不存在！");
			return apiResultModel;
		}

		// 不能删除会籍顾问经理 只有后台管超级管理员才有权限
		if ("admin".equals(presaleTeamUserOld.getRole())) {
			//
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("您没有该权限！");
			return apiResultModel;
		}
		
		presaleTeamUserOld.setStatus(0);
		presaleTeamUserOld.setUpdateBy(adminUserInfo.getId()+"");
		presaleTeamUserOld.setUpdateTime(new Date());
		
		Integer updateCount = tSysPresaleTeamMapper.updateSelectiveById(presaleTeamUserOld);
		if(updateCount > 0) {
			List<PresaleTeamUserApiModel> teamList = tSysPresaleTeamMapper.queryPresaleTeamAll(adminUserInfo.getId());	
			resultMap.put("presaleTeamList", teamList);
			apiResultModel.setData(resultMap);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("删除成功");
		}else {
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("删除失败");
		}
		
		return apiResultModel;
	}
	
	
	/**
	 * 获取到店咨询数据
	 */
	@Override
	public ApiResultModel<Object> queryStoreAdvisoryList(UserInfo adminUserInfo, String payDate) {
		ApiResultModel<Object> apiResultModel = new ApiResultModel<>();
		TSysPresaleTeam presaleTeamUser = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(null, adminUserInfo.getMobile());
		Map<String,Object> resultMap = new HashMap<>();
		if(presaleTeamUser != null && "admin".equals(presaleTeamUser.getRole())) {
			// 有权限查看
			List<OrderCoachPrivate> list = tSysPresaleTeamMapper.queryStoreAdvisoryList(payDate);
			resultMap.put("list", list);
			resultMap.put("isAdmin", true);
			apiResultModel.setData(resultMap);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询成功");
		}else if(presaleTeamUser != null && "user".equals(presaleTeamUser.getRole())) {
			// 普通用户
			resultMap.put("isAdmin", false);
			apiResultModel.setData(resultMap);
			apiResultModel.setRet(ApiResultModel.RET_SUCCESS);
			apiResultModel.setMsg("查询成功");
		}else {
			// 没有权限
			apiResultModel.setRet(ApiResultModel.RET_ERROR);
			apiResultModel.setMsg("你没有权限");
		}
		
		return apiResultModel;
	}
	
	/**
	 * 校验是不是预售人员
	 */
	@Override
	public boolean checkPresalTeamUser(Integer userId, String mobile) {
		// 
		if(StringUtils.isEmpty(mobile)) return false;
		if(userId == null || userId <= 0 ) return false;
		
		TSysPresaleTeam presaleTeamUser = tSysPresaleTeamMapper.queryTSysPresaleTeamByUserIdOrMobile(userId, mobile);
		boolean hasPresale = true;
		if(presaleTeamUser == null ) {
			return false;
		}
		
		hasPresale = presaleTeamUser.getStatus() > 0 ? true : false;
		return hasPresale;
	}
	
	


}